<?php

$SCRIPT_LOC = "./convert_to_sql.pl";
$DB_HOST = "localhost";
$DB_USERNAME = "root";
$DB_PASSWORD = "itgftw";
$DB_NAME = "nlp";

$DELETE_SQL = "DELETE FROM Event";
$FACTS_SQL = "SELECT * FROM Event";



$dbLink = @mysql_connect($DB_HOST, $DB_USERNAME, $DB_PASSWORD);
$dkpDB = @mysql_select_db($DB_NAME, $dbLink);

$hasError = false;

if (isset($_GET["action"])) {
  $action = $_GET["action"];

  if ($action == "input") {
 
  
    $input = $_GET["input"];
    $lastCommand = $input;
    
    $escaped = escapeshellarg($input);
    
    $cmd = $SCRIPT_LOC . " " . $escaped;
    
    unset($output);
    
    exec($cmd, $output, $retval);  
    
    if ($retval == 0 and $output[0] != "NO PARSES") {
      $type = $output[0];
      $sql = $output[1];
    
      $queryResults = @mysql_query($sql, $dbLink) or die(mysql_error());
      
      $resultArray = array();
      $numrows = @mysql_num_rows($queryResults);
      
      if ($numrows < 1) {
	$resultStr = "Database returned no results";
	$hasResults = false;
      } else {
	for ($i = 0; $i < $numrows; $i++) {
	  $resultArray[] = @mysql_fetch_array($queryResults, MYSQL_ASSOC);
	}
	$hasResults = true;
      }    
    } else {
      $errorStr = "Unable to translate the command.";
      $hasError = true;
    }
  } elseif ($action == "clear") {
    $sql = $DELETE_SQL;
    $queryResults = @mysql_query($sql, $dbLink) or die(mysql_error());
  }
}


function writeTable($arr, $border, $showHeaders) {
  if (count($arr) < 1) {
    return;
  }

  $brd = 1;
  if (isset($border)) {
    $brd = $border;
  } 
    
  
  echo "<table border=$brd>\n";

  if (isset($showHeaders) and $showHeaders) {
    $headers = array_keys($arr[0]);

  
    echo "<tr>";
    foreach ($headers as $header) {
      echo "<th>$header</th>";
    }
    echo "</tr>\n";
  }

  foreach ($arr as $row) {
    echo '<tr>';
    foreach ($row as $v) {
      $s = $v == null ? '&nbsp;' : $v;
      echo "<td>$s</td>";
    }
    echo "</tr>\n";
    $first = false;
  }
  echo "</table>\n";
}

?>


<html>

<head>
  <title>6.863 Project - Eric Lieberman and Mike Lin</title>
</head>

<style>


pre, code, .answer {
  font-family: Arial;
  font-size: 12pt;
  background-color: #F0F0F0;
}

code {
  padding: 2px 2px;
}

pre, .answer {
 padding: 3px 3px;
 border: 1px dotted #999999;
 margin-left: 20px;
 margin-right: 20px;
}

</style>


<body>
<table border=0 width=800>
<tr><td>
<font face="Arial">

<b>
<font size=6>6.863 Project</font>
<br>
<font size=4>Eric Lieberman and Mike Lin</font>
</b>
<p>

<?php

if (isset($lastCommand)) {
  echo "Last Sentence:<br>";
  echo '<pre>';
  echo $lastCommand;
  echo '</pre>';
}

if ($hasError) {
  echo "<font color=red>$errorStr</font>";
} elseif ($action == "input") {
  if (isset($sql)) {
    echo 'Output SQL:<br>';
    echo '<pre>';
    echo $sql;
    echo '</pre>';
  } 
  if ($type == "fact") {
    echo '<font color=red>Inserted the fact into the database.</font>';
  } elseif ($type == "ynQuestion") {
    echo 'Yes/No Question Answer:<br>';
    echo '<div class="answer">';
    writeTable($resultArray, 0, false);   
    echo '</div>';
  } elseif ($type == "whQuestion") {
    echo 'Question Answer:<br>';
    echo '<div class="answer">';
    writeTable($resultArray, 0, false);
    echo '</div>';
  }
} elseif ($action == "clear") {
  echo '<font color=red>';
  echo 'Cleared the database!';
  echo '</font>';
}

?>




<p>
Please type in a sentence!
<form name="nlp" action="index.php" method="GET">
  <input type="textbox" size=80 name="input">
  <input type="hidden" name="action" value="input">
  <input type="submit" value="Submit">
</form>


<p>

<font size=4><b>Database Information</b></font>
<br>

<?php
$queryResults = @mysql_query($FACTS_SQL, $dbLink);

$dbRows = array();
$numDbRows = @mysql_num_rows($queryResults);

for ($i = 0; $i < $numDbRows; $i++) {
  $dbRows[] = @mysql_fetch_array($queryResults, MYSQL_ASSOC);
}
echo "Database currently has $numDbRows row(s):";

writeTable($dbRows, 1, true);


?>

<form name="nlp" action="index.php" method="GET">
  <input type="submit" value="Clear Database">
  <input type="hidden" name="action" value="clear">
</form>


</font>
</td></tr></table>
</body>

</html>

